package com.zj.weather.bgviews;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.GradientDrawable;

import java.util.ArrayList;
import java.util.List;

import com.zj.weather.base.BaseHolder;
import com.zj.weather.base.BaseWeatherBgView;
import com.zj.weather.base.HoldersInfo;
import com.zj.weather.utils.DisplayUtils;

import static com.zj.weather.utils.RandomUtils.getDownRandFloat;
import static com.zj.weather.utils.RandomUtils.getRandMax;
import static com.zj.weather.utils.RandomUtils.getRandom;

/**
 * 晴天/夜
 */
public class StarDrawer extends BaseWeatherBgView {
    private GradientDrawable drawable;
    private static final int STAR_COUNT = 100;
    private static final float STAR_MIN_SIZE = 1.5f;
    private static final float STAR_MAX_SIZE = 4f;

    public StarDrawer(Context context) {
        super(context);
        drawable = new GradientDrawable(GradientDrawable.Orientation.BL_TR, new int[]{0xffffffff, 0x90aaaaff});
        drawable.setShape(GradientDrawable.OVAL);
        drawable.setGradientType(GradientDrawable.RADIAL_GRADIENT);
        drawable.setGradientRadius((float) (Math.sqrt(2) * 60));
    }

    @Override
    protected List<HoldersInfo> getHolders() {
        List<HoldersInfo> infos = new ArrayList<>();
        infos.add(new HoldersInfo(STAR_COUNT) {
            @Override
            public StarHolder getHolderType(int position) {
                final float maxBottom = height * starPaddingBottom;
                final float y = STAR_MAX_SIZE * 2f + getRandMax((maxBottom - STAR_MAX_SIZE * 2f), 1.5f);
                // 15%的上半部分屏幕最高alpha为1，其余的越靠下最高alpha越小
                final float maxAlpha = 0.15f + 0.85f * (1f - y / maxBottom);
                final float starSize = getRandMax((dp2px(STAR_MAX_SIZE) - dp2px(STAR_MIN_SIZE)), 1.2f) + dp2px(STAR_MIN_SIZE);
                return new StarHolder(context, getRandom(0, (width - starSize * 2f)) + starSize, y, starSize, maxAlpha, position);
            }
        });
        return infos;
    }

    //数值越大，星星越靠下
    private final float starPaddingBottom = 0.5f;

    public class StarHolder extends BaseHolder {

        private final float maxAlpha;
        private final float startSize;
        private float curAlpha;
        private boolean alphaIsGrowing = true;
        private float delta;

        public StarHolder(Context context, float x, float y, float startSize, float maxAlpha, int position) {
            super(x, y, position);
            this.maxAlpha = maxAlpha;
            this.startSize = startSize;
            this.curAlpha = getRandom(0f, maxAlpha);
            delta = getRandom(0.0013f * maxAlpha, 0.012f * maxAlpha);
        }

        @Override
        public void initData(Context context) {

        }

        @Override
        public void updateFrame(Context context, Canvas canvas, float changedAlpha) {

            if (alphaIsGrowing) {
                curAlpha += delta;
                if (curAlpha > maxAlpha) {
                    curAlpha = maxAlpha;
                    alphaIsGrowing = false;
                }
            } else {
                curAlpha -= delta;
                if (curAlpha < 0) {
                    curAlpha = 0;
                    alphaIsGrowing = true;
                }
            }
            final int left = Math.round(canvasWidth - startSize / 2f);
            final int right = Math.round(canvasWidth + startSize / 2f);
            final int top = Math.round(canvasHeight - startSize / 2f);
            final int bottom = Math.round(canvasHeight + startSize / 2f);
            drawable.setBounds(left, top, right, bottom);
            drawable.setGradientRadius(startSize / 2f);
            drawable.setAlpha((int) (255 * curAlpha * changedAlpha));
            drawable.draw(canvas);
        }
    }
}
